package edu.fight.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import edu.fight.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@RestController
@RequestMapping("/pro")
@Slf4j
public class IndexController {

    /**
     * 这是保护资源
     * @return
     */
    @GetMapping("/resouces")
    public String hello(){
        return "hello  保护资源";
    }


    @RequiresRoles("admin")
    @GetMapping("/show")
    public String show(HttpSession session){
        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()){
            String key = attributeNames.nextElement().toString();
            Object value = session.getAttribute(key);
            System.out.println(String.format("key : %s , value : %s ", key,value));

        }
        return "成功";
    }

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/getself")
    public String getself(){
        SimpleAuthenticationInfo houlu = redisUtil.get("houlu");
        log.info(JSONObject.toJSONString(houlu, SerializerFeature.PrettyFormat));
        return String.valueOf(houlu.getPrincipals().getPrimaryPrincipal());
    }

}
